﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;
using NewGlobalBizAssist.Lib;
using System.Data;
 

namespace NewGlobalBizAssist
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private bool ValidateUser(string userName, string passWord)
        {


 

            // Check for invalid userName.
            // userName must not be null and must be between 1 and 15 characters.
            if ((null == userName) || (0 == userName.Length) || (userName.Length > 15))
            {
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.");
                return false;
            }

            // Check for invalid passWord.
            // passWord must not be null and must be between 1 and 25 characters.
            if ((null == passWord) || (0 == passWord.Length) || (passWord.Length > 25))
            {
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.");
                return false;
            }

            try
            {
                Lib.User usr = new Lib.User();
                DataRow dr = usr.LogIn(userName, passWord);
                if (dr != null) return true;
                return false;
            }
            catch (Exception ex)
            {
                // Add error handling here for debugging.
                // This error message should not be sent back to the caller.
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
                return false;
            }

          

        }


        protected void cmdLogin_ServerClick(object sender, System.EventArgs e)
        {


            if (ValidateUser(txtUserName.Value, txtUserPass.Value))
                FormsAuthentication.RedirectFromLoginPage(txtUserName.Value,
                    chkPersistCookie.Checked);
            else
                Response.Redirect("login.aspx", true);


          //  if (ValidateUser(txtUserName.Value, txtUserPass.Value))
          //  {
          //      FormsAuthenticationTicket tkt;
          //      string cookiestr;
          //      HttpCookie ck;
          //      tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now,
          //DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data");
          //      cookiestr = FormsAuthentication.Encrypt(tkt);
          //      ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
          //      if (chkPersistCookie.Checked)
          //          ck.Expires = tkt.Expiration;
          //      ck.Path = FormsAuthentication.FormsCookiePath;
          //      Response.Cookies.Add(ck);

          //      string strRedirect;
          //      strRedirect = Request["ReturnUrl"];
          //      if (strRedirect == null)
          //          strRedirect = "default.aspx";
          //      Response.Redirect(strRedirect, true);
          //  }
          //  else
          //      Response.Redirect("login.aspx", true);




            //FormsAuthentication.SetAuthCookie(
            //     this.TextBox_username.Text.Trim(), flase);

            //FormsAuthenticationTicket ticket1 =
            //   new FormsAuthenticationTicket(
            //        1,                                   // version
            //        this.TextBox_username.Text.Trim(),   // get username  from the form
            //        DateTime.Now,                        // issue time is now
            //        DateTime.Now.AddMinutes(10),         // expires in 10 minutes
            //        false,      // cookie is not persistent
            //        "HR"                              // role assignment is stored
            //    // in userData
            //        );
            //HttpCookie cookie1 = new HttpCookie(
            //  FormsAuthentication.FormsCookieName,
            //  FormsAuthentication.Encrypt(ticket1));
            //Response.Cookies.Add(cookie1);

            //// 4. Do the redirect. 
            //String returnUrl1;
            //// the login is successful
            //if (Request.QueryString["ReturnUrl"] == null)
            //{
            //    returnUrl1 = "HRpages/HR_main.aspx";
            //}

            ////login not unsuccessful 
            //else
            //{
            //    returnUrl1 = Request.QueryString["ReturnUrl"];
            //}
            //Response.Redirect(returnUrl1);


        }




  

    }
}